package applications.studentApplicationForm.ui.lib;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.util.Observable;

import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

import org.objectweb.fractal.fraclet.annotations.Attribute;
import org.objectweb.fractal.fraclet.annotations.Component;
import org.objectweb.fractal.fraclet.annotations.Interface;
import org.objectweb.fractal.fraclet.annotations.Requires;

import applications.studentApplicationForm.fc.api.StudentForm;

@Component(provides = {@Interface(name = "jPanelItf", signature = org.objectweb.fractal.swing.JPanelItf.class), @Interface(name = "jPanelAttributes", signature = org.objectweb.fractal.swing.JPanelAttributes.class), @Interface(name = "uicontainerservice", signature = ontocompo.api.UIContainerService.class), @Interface(name = "ontocompointrospection", signature = ontocompo.api.OntoCompoIntrospection.class)})


public class StudentApplicationFormSubjectPanel extends JPanel  implements  java.util.Observer, org.objectweb.fractal.swing.JPanelItf, org.objectweb.fractal.swing.JPanelAttributes, ontocompo.api.UIContainerService, ontocompo.api.OntoCompoIntrospection {


	private String uid;
	private org.objectweb.fractal.api.Component c;
	private JTable table;

	
	@Attribute
	private int widthCell;
	
	@Attribute
	private int heightCell;
	
	@Requires(name="pane")
	private PanelInterface mypane;
	
	@Requires(name="subjectcodetitle")
	private JLabelInterface subjectcodetitle;
	
	@Requires(name="subjectnametitle")
	private JLabelInterface subjectnametitle;
	
	@Requires(name="subjectnotetitle")
	private JLabelInterface subjectnotetitle;

	@Requires(name="subjectvalidationtitle")
	private JLabelInterface subjectvalidationtitle;

	@Requires(name="subjectcode")
	private PanelColInterface subjectcode;
	
	@Requires(name="subjectname")
	private PanelColInterface subjectname;
	
	@Requires(name="subjectnote")
	private PanelColInterface subjectnote;

	@Requires(name="subjectvalidation")
	private PanelColInterface subjectvalidation;
	

	@Requires(name = "studentformdirectoryservice")
	private applications.studentApplicationForm.fc.api.StudentFormDirectoryService studentformdirectoryservice;

	@Requires(name = "studentformdirectoryobservable")
	private ontocompo.api.Observable studentformdirectoryobservable;


	public StudentApplicationFormSubjectPanel() {
		super();
		this.uid = "applications.studentApplicationForm.ui.lib.StudentApplicationFormSubjectPanel";
		System.out.println(this.toString() + " created");
	}




	@Override
	public void init() {
		// TODO Auto-generated method stub

		this.studentformdirectoryobservable.addObserver(this);

		this.setLayout(new BorderLayout());
		this.setBackground(Color.green);



		this.add("North", createTh("Subjects"));

		System.out.println("coucou -1 "+widthCell+" "+ heightCell);
		this.add("Center", mypane.getComponentItSelf());
		mypane.init();
		mypane.setLayout(new BoxLayout(mypane.getComponentItSelf(), BoxLayout.Y_AXIS));
		Dimension dim = new Dimension((widthCell+subjectcode.getMarge()*2)*4, heightCell*7+subjectcode.getMarge()*2);

		modifyComponent(mypane.getComponentItSelf(), dim);
		
		
		JPanel titres = new JPanel();
		titres.setLayout(new BoxLayout(titres, BoxLayout.X_AXIS));
		Dimension d_titres = new Dimension((widthCell+subjectcode.getMarge()*2)*4, heightCell);
		modifyComponent(titres, d_titres);
		
		Dimension d_elements = new Dimension(widthCell+subjectcode.getMarge()*2, heightCell);
		modifyComponent(subjectcodetitle.getComponentItSelf(), d_elements);
		titres.add(subjectcodetitle.getComponentItSelf());
		
		modifyComponent(subjectnametitle.getComponentItSelf(), d_elements);
		titres.add(subjectnametitle.getComponentItSelf());
		
		modifyComponent(subjectnotetitle.getComponentItSelf(), d_elements);
		titres.add(subjectnotetitle.getComponentItSelf());
		
		modifyComponent(subjectvalidationtitle.getComponentItSelf(), d_elements);
		titres.add(subjectvalidationtitle.getComponentItSelf());
		
		mypane.add(titres);
		
		JPanel colonnes = new JPanel();
		Dimension d_col = new Dimension((widthCell+subjectname.getMarge()*2)*4, heightCell*6+subjectname.getMarge()*2);
		modifyComponent(colonnes, d_col);

		colonnes.setLayout(new BoxLayout(colonnes, BoxLayout.X_AXIS));
		subjectcode.init();
		colonnes.add(subjectcode.getComponentItSelf());
		subjectname.init();
		colonnes.add(subjectname.getComponentItSelf());
		subjectnote.init();
		colonnes.add(subjectnote.getComponentItSelf());
		subjectvalidation.init();
		colonnes.add(subjectvalidation.getComponentItSelf());
		
		mypane.add(colonnes);

	}

	protected JLabel createTh(String txt)
	{
		JLabel result = new JLabel(txt);
		result.setHorizontalAlignment(SwingConstants.CENTER);
		return result;
	}

	@Override
	public void update(Observable o, Object arg) {

		// TODO Auto-generated method stub

		StudentForm bk = this.studentformdirectoryservice.getStudentForm();

		if (bk == null){
			return;
		}
		else { 
			this.update(bk);
		}


	}

	@Override
	public JComponent getComponentItSelf() {
		// TODO Auto-generated method stub
		return this;
	}

	@Override
	public void update(Object o) {		

//		this.removeAll();
//		this.setBackground(Color.white);
//
//
//		JLabel lbl  = createTh("Subject");
//		lbl.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
//
//		JPanel panelContenu = new JPanel(new BorderLayout());
//		panelContenu.setBackground(Color.white);
//
//
//		JPanel panelNorth = new JPanel();
//		panelNorth.setBackground(Color.white);
//
//		panelNorth.add(lbl);
//
//		JPanel panelCenter = new JPanel();
//		panelCenter.setBackground(Color.white);
//
//
//		final String[] columnNames = this.studentformdirectoryservice.getSubjectTitles();
//
//
//		// par defaut valeur == null
//		if (this.studentformdirectoryservice.getYearValue() == null)
//		{
//
//
//			JScrollPane scrollPane = new JScrollPane();				
//			panelCenter.add(scrollPane);
//
//			panelContenu.add(panelNorth, BorderLayout.CENTER);
//
//			this.add(panelContenu, BorderLayout.SOUTH);
//
//		}
//
//
//		// on recupere l'annee lors du click
//		else
//		{
//			final String[][] data = this.studentformdirectoryservice.getSubjectData(this.studentformdirectoryservice.getYearValue());
//
//			// on utilise le tableModel pour empecher que l'utilisateur modifie les donnees lors du click
//			TableModel dataModel = new AbstractTableModel() {
//				public int getColumnCount() { return columnNames.length; }
//				public int getRowCount() { return data.length;}
//				public Object getValueAt(int row, int col) {
//					return data[row][col];
//				}
//				public String getColumnName(int column) {return columnNames[column];}
//				public Class getColumnClass(int col) {
//					return getValueAt(0,col).getClass();
//				}
//				public void setValueAt(String aValue, int row, int column) {
//					data[row][column] = aValue;
//				}
//			};
//
//			table = new JTable(dataModel);
//			table.setPreferredScrollableViewportSize(new Dimension(800, 190));
//			table.setFillsViewportHeight(true);
//			table.setForeground(Color.BLACK);
//			table.setRowHeight(25);
//
//			JScrollPane scrollPane = new JScrollPane();
//			scrollPane.setViewportView(table);
//
//
//
//			panelCenter.add(scrollPane);
//
//			panelContenu.add(panelNorth, BorderLayout.NORTH);
//			panelContenu.add(panelCenter, BorderLayout.CENTER);
//
//			this.add(panelContenu, BorderLayout.SOUTH);
//		}


		String[] columnNames = this.studentformdirectoryservice.getSubjectTitles();
		
		System.out.println(columnNames);
		System.out.println(columnNames.length);

		subjectcodetitle.setText(columnNames[0]);
		subjectnametitle.setText(columnNames[1]);
		subjectnotetitle.setText(columnNames[2]);
		subjectvalidationtitle.setText(columnNames[3]);
		
		String[][] data = this.studentformdirectoryservice.getSubjectData(this.studentformdirectoryservice.getYearValue());
		
		for(int k=0; k < data.length; k++)
			for(int l=0; l < data[k].length; l++)
				System.out.println("data["+k+"]["+l+"] = "+data[k][l]);
		
		for(int i = 0; i < data.length; i++)
		{
			if (data[i][0] == null) subjectcode.setText("", i);
			else subjectcode.setText(data[i][0], i);
			
			if (data[i][1] == null) subjectname.setText("", i);
			else subjectname.setText(data[i][1], i);
			
			if (data[i][2] == null) subjectnote.setText("", i);
			else subjectnote.setText(data[i][2], i);
			
			if (data[i][3] == null) subjectvalidation.setText("", i);
			else subjectvalidation.setText(data[i][3], i);
		}


		
		this.validate();  // pour rafraichir la page
		repaint();
		this.getParent().repaint();  // pour rafraichir la page
		this.getParent().validate();  // pour rafraichir la page
		System.out.println(getSize());
		System.out.println(getParent().getClass());


	}

	public String toString() {
		return "StudentApplicationFormSubjectPanel";
	}




	@Override
	public org.objectweb.fractal.api.Component getFractalComponent() {
		// TODO Auto-generated method stub
		return this.c;
	}

	@Override
	public String getUid() {
		// TODO Auto-generated method stub
		return this.uid;
	}

	@Override
	public void setFractalComponent(org.objectweb.fractal.api.Component c) {
		// TODO Auto-generated method stub
		this.c = c;
	}

	@Override
	public void setUid(String uid) {
		// TODO Auto-generated method stub
		this.uid = uid;
	}


	private void modifyComponent(JComponent c, Dimension d)
	{
		c.setSize(d);
		c.setPreferredSize(d);
		c.setMaximumSize(d);
		c.setMinimumSize(d);
	}


}
